home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / NeoIntroAlone3.0 folder / Standalone / Laughs / Source / CLaughsApp.cp next >
Encoding:
Text File  |  1994-09-26  |  5.3 KB  |  231 lines  |  [TEXT/KAHL]

  1. /*     File name    : CLaughsApp.cp
  2.     Author        : Paul Gee
  3.     Evolution    : Bob Krause
  4.     Version        : 0.2
  5.     Date        : 6th April 1993
  6.     Purpose        : Simple NeoAccess Demo.
  7.                   Run it the first time and it creates a new database
  8.                   and stores some objects.
  9.                   Run it a second time and it reads the objects back
  10.                   in and prints out their details.
  11. */
  12.  
  13. #include "NeoTypes.h"
  14. #include CNeoMetaClassH
  15. #include CNeoDatabaseNativeH
  16. #include CNeoIDListH
  17. #include CNeoIndexIteratorH
  18. #include CNeoSelectH
  19. #include "CLaughsApp.h"
  20. #include "CPerson.h"
  21. #include "CNameIndex.h"
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <time.h>
  25.  
  26. #define kLaughsCreator            'Neo6'
  27. #define kLaughsFileType            'Ne6d'
  28.  
  29. int main(void)
  30. {
  31.     CLaughsApp *    app;
  32.  
  33.     app = new CLaughsApp;
  34.     app->run();
  35.     delete app;
  36.  
  37.     return 0;
  38. }
  39.  
  40. /***
  41.  * CLaughsApp
  42.  *
  43.  *    Initialize the application. If your application class defines its
  44.  *    own instance variables or global variables, this is a good place
  45.  *    to initialize them.
  46.  *
  47.  ***/
  48. CLaughsApp::CLaughsApp(void)
  49. {
  50.     NeoPerms            permissions;
  51.     CNeoMetaClass *        meta;
  52.  
  53.     printf("Start Laughing...\n\n");
  54.         
  55.     meta = new CNeoMetaClass(kNameIndexID, kNeoNullClassID, "\pCNameIndex", CNameIndex::New, CNameIndex::KeyManager);
  56.  
  57.     meta = new CNeoMetaClass(kNeoIDListID, kNeoNullClassID, "\pCNeoIDList", CNeoIDList::New, CNeoIDList::KeyManager);
  58.  
  59.     meta = new CNeoMetaClass(kPersonID, kNeoPersistID, "\pCPerson", CPerson::New);
  60.     meta->setKey(kNeoSecondaryIndex, kNameIndexID);
  61.  
  62.     meta = new CNeoMetaClass(kJokerID, kPersonID, "\pCJoker", CJoker::New);
  63.     meta->setKey(kNeoSecondaryIndex, kNameIndexID);
  64.  
  65.     meta = new CNeoMetaClass(kJokeID, kNeoPersistID, "\pCJoke", CJoke::New);
  66.  
  67.     meta = new CNeoMetaClass(kClownID, kPersonID, "\pCClown", CClown::New);
  68.     meta->setKey(kNeoSecondaryIndex, kNameIndexID);
  69.  
  70.     meta = new CNeoMetaClass(kPieID, kNeoPersistID, "\pCPie", CPie::New);
  71.  
  72.     gNeoDatabase = new CNeoDatabaseNative(kLaughsCreator, kLaughsFileType);
  73.     ((CNeoDatabaseNative *)gNeoDatabase)->Specify("\pLaughter", 0);
  74.  
  75.     if (gNeoDatabase->existsOnDisk())
  76.         permissions = NeoReadPerm;
  77.     else {
  78.         gNeoDatabase->create();
  79.         permissions = NeoReadWritePerm;
  80.     }
  81.     gNeoDatabase->open(permissions);
  82. }
  83.  
  84. CLaughsApp::~CLaughsApp(void)
  85. {
  86.     gNeoDatabase->close();
  87.     delete gNeoDatabase;
  88.     gNeoDatabase = nil;
  89. }
  90.  
  91. void CLaughsApp::run(void)
  92. {
  93.     if (gNeoDatabase->getObjectCount(kPersonID, TRUE) > 0)
  94.         printOut();
  95.     else {
  96.         createObjects();
  97.         gNeoDatabase->commit();
  98.     }
  99.     printf("\n\nDone!\n");
  100. }
  101.  
  102. /***
  103.  * createObjects
  104.  *
  105.  *    Add three people to the database, two jokers and a clown.
  106.  *
  107.  ***/
  108. void CLaughsApp::createObjects(void)
  109. {
  110.     short                index;
  111.     CJoke *                joke1;
  112.     CJoke *                joke2;
  113.     CJoker *            joker;
  114.     CClown *            clown;
  115.     CNeoDatabaseNative *database    = (CNeoDatabaseNative *)gNeoDatabase;
  116.     CNeoString            name;
  117.  
  118.     database->getName(name);
  119.     name[name.getLength() +1] = 0;
  120.     printf("Store 2 Jokers & a Clown in \"%s\".\n", &name[1]);
  121.  
  122.     // Know any good jokes? How 'bout this one...
  123.     joke1 = new CJoke("The world’s shortest poem: Flees. Adam had'em.");
  124.  
  125.     // Add it to the database.
  126.     // Note: An object ID is assigned automaticly by addObject.
  127.     database->addObject(joke1);
  128.  
  129.     // Is this a joke???
  130.     joke2 = new CJoke("My dogs got no nose?");
  131.  
  132.     // Add it to the database.
  133.     database->addObject(joke2);
  134.  
  135.     // Create a joker object.
  136.     joker = new CJoker("\pJack");
  137.  
  138.     // Teach it a couple of jokes.
  139.     joker->learnJoke(joke1);
  140.     joker->learnJoke(joke2);
  141.  
  142.     // Add it to the database.
  143.     database->addObject(joker);
  144.  
  145.     // Don't need this guy any more. Remove our reference to it.
  146.     joker->unrefer();
  147.     joker = nil;
  148.  
  149.     // Create a clown.
  150.     clown = new CClown("\pFred");
  151.  
  152.     // Add it to the database.
  153.     database->addObject(clown);
  154.  
  155.     // Build up its arsenal.
  156.     clown->bakePie("Jello");
  157.     clown->bakePie("Marshmellow");
  158.     clown->bakePie("Custard");
  159.     clown->bakePie("Cool Whip®");
  160.     clown->bakePie("Yogurt");
  161.  
  162.     // Remember to remove our reference when we're done.
  163.     clown->unrefer();
  164.     clown = nil;
  165.  
  166.     // Create another joker.
  167.     joker = new CJoker("\pHarry");
  168.  
  169.     // Add it to the database.
  170.     database->addObject(joker);
  171.  
  172.     // This guy steals jokes.
  173.     joker->learnJoke(joke2);
  174.  
  175.     // Remove our reference to the joker and the jokes.
  176.     joker->unrefer();
  177.     joker = nil;
  178.  
  179.     for (index = 0; index < 200; index++) {
  180.         joker = new CJoker("\pHarry");
  181.         joker->learnJoke(joke2);
  182.         database->addObject(joker);
  183.         joker->unrefer();
  184.         if ((index&0x7F) == 0)
  185.             database->commit(FALSE);
  186.     }
  187.  
  188.  
  189.     joke1->unrefer();
  190.     joke1 = nil;
  191.     joke2->unrefer();
  192.     joke2 = nil;
  193. }
  194.  
  195. /***
  196.  * printOut
  197.  *
  198.  *    Find in turn each of the three objects in the database, two jokers and
  199.  *    a clown, then commit the changes to disk.
  200.  *
  201.  ***/
  202. void CLaughsApp::printOut(void)
  203. {
  204.     CPerson *            person;
  205.     CNeoNameSelect        key("\p");
  206.     CNeoDatabaseNative *database    = (CNeoDatabaseNative *)gNeoDatabase;
  207.     CNeoIndexIterator    iterator(database, kPersonID, &key, TRUE);
  208.     CNeoString            name;
  209.  
  210.     // Tell them what we're about to do.
  211.     database->getName(name);
  212.     name[name.getLength() +1] = 0;
  213.     printf("Restoring %ld Jokers & %ld Clowns from \"%s\".\n\n",
  214.         database->getObjectCount(kJokerID, FALSE),
  215.         database->getObjectCount(kClownID, FALSE), &name[1]);
  216.     
  217.     // Let's make rand() a little more variable.
  218.     srand((int)time(nil));
  219.  
  220.     // Get the first person object.
  221.     person = (CPerson *)iterator.currentObject();
  222.  
  223.     while(person) {
  224.         person->printName();                        // Introductions.
  225.         person->skill();                            // Entertain the crowd.
  226.         printf("\n");
  227.         person = (CPerson *)iterator.nextObject();    // Next.
  228.     }
  229. }            
  230.  
  231.